﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

namespace StarForce
{
    /// <summary>
    /// 长按按钮
    /// </summary>
    public class LongPressButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    {
        //按下多长时间算长按
        [SerializeField]private float timeLongPress = 0.5f;
 
        //是否按下
        private bool isPointerDown = false;
 
        //按下时刻
        private float timePointerDown = 0;
 
        //短按回调
        public UnityEvent MethodCallBack1;
        
        //长按回调
        public UnityEvent MethodCallBack2;

        public void OnPointerDown(PointerEventData eventData)
        {
            isPointerDown = true;
            timePointerDown = Time.time;
        }
        
        public void OnPointerUp(PointerEventData eventData)
        {
            float span = Time.time - timePointerDown;
            if (span > timeLongPress)
            {
                MethodCallBack2.Invoke();
            }
            else
            {
                MethodCallBack1.Invoke();
            }
        }
    }

}